package com.hschinese.pronunciation.utils;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;

import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * Created by wo on 2015/11/18.
 */
public class AnimatorUtil {
    public static void slideIn(View view, long durationMillis, long delayMillis, Animation.AnimationListener listener) {
        TranslateAnimation animation = new TranslateAnimation(2, 1, 2, 0, 2, 0, 2, 0);
        baseIn(view, animation, durationMillis, delayMillis);
        if (listener != null) {
            animation.setAnimationListener(listener);
        }
    }

    private static void baseIn(View view, Animation animation, long durationMillis, long delayMillis){
        animation.setDuration(durationMillis);
        animation.setStartOffset(delayMillis);
        view.setVisibility(View.VISIBLE);
        view.startAnimation(animation);
    }

    public static void slideOut(View view, long durationMillis, long delayMillis, Animation.AnimationListener listener) {
        TranslateAnimation animation = new TranslateAnimation(2, 0, 2, 1, 2, 0, 2, 0);
        baseOut(view, animation, durationMillis, delayMillis);
        if (listener != null) {
            animation.setAnimationListener(listener);
        }
    }

    private static void baseOut(View view, Animation animation, long durationMillis, long delayMillis){
        animation.setDuration(durationMillis);
        animation.setStartOffset(delayMillis);
        view.startAnimation(animation);
    }

    public static void pluseAnimator(Context cxt, View view) {
        AnimatorSet mAnimatorSet = new AnimatorSet();
        mAnimatorSet.setDuration(1000);
        mAnimatorSet.setInterpolator(new LinearInterpolator());
        mAnimatorSet.playTogether(
                ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),
                ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
        );
        mAnimatorSet.start();
    }

    public static void shakeAnimator(Context cxt, View view) {
        AnimatorSet mAnimatorSet = new AnimatorSet();
        mAnimatorSet.setDuration(1000);
        mAnimatorSet.setInterpolator(new LinearInterpolator());
        mAnimatorSet.playTogether(
                ObjectAnimator.ofFloat(view, "translationX", 0, 25, -25, 25, -25, 15, -15, 6, -6, 0)
        );
        mAnimatorSet.start();
    }
}
